package com;

/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2021/11/1 16:10
 */

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


/***
 * 流处理
 */
public class StreamWordCount {
    public static void main(String[] args) throws  Exception{
        //创建流处理执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //从文件中读取数据
        /*String inputPath ="src/main/resources/test.txt";
        DataStream<String> inputDataStream = env.readTextFile(inputPath);*/

        //使用parameter tool 工具从启动参数中提取配置项
        ParameterTool parameterTool = ParameterTool.fromArgs(args);
        String host = parameterTool.get("host");
        int port = parameterTool.getInt("port");


        //从socket文本流读取数据
        DataStream<String> inputDataStream = env.socketTextStream(host, port);

        //基于数据流进行抓换计算
        SingleOutputStreamOperator<Tuple2<String, Integer>> resultStream = inputDataStream.flatMap(new WordCount.MyFlatMapper())
                .keyBy(0)
                .sum(1);

        //输出,设置并行度为1
        resultStream.print().setParallelism(1);

        //执行任务
        env.execute();

    }
}
